#encoding: utf-8
#用来将lib里的静态数据迁移到tutem_mall.eb_daily_poster中

require 'json'
require 'awesome_print'

load 'rds_api.rb'

def history_json_to_recs

  poems = {
    "2022-02-07": {
      "title": "元月初七",
      "author": "[唐]罗隐",
      "body": [
        "一二三四五六七，",
        "万物生芽是今日。",
        "远天归雁拂云飞，",
        "近水游鱼迸水出。"
      ]
    },
    "2022-02-08": {
      "title": "早春寄王汉阳",
      "author": "[唐]李白",
      "body": [
        "闻道春还未相识，走傍寒梅访消息。",
        "昨夜东风入武阳，陌头杨柳黄金色。",
        "碧水浩浩云茫茫，美人不来空断肠。",
        "预拂青山一片石，与君连日醉壶觞。"
      ],
      "poster": "https://oss.foodtrust.cn//eb89720220208084357758.jpg"
    },
    "2022-02-09": {
      "title": "早春野望",
      "author": "[唐]王勃",
      "body": [
        "江旷春潮白，山长晓岫青。",
        "他乡临睨极，花柳映边亭。"
      ],
      "poster": "https://oss.foodtrust.cn//f159e202202082015393441.jpg"
    },
    "2022-02-10": {
      "title": "风送行春步",
      "author": "[南宋]黄升",
      "body": [
        "风送行春步。",
        "渐行行、山回路转，入云深处。",
        "问讯花梢春几许，半在诗人杖屦。",
        "点点是、祥烟膏露。",
        "中有瑶池千岁种，整严妆、来作巢仙侣。",
        "相妩媚，试凝伫。",
        "风流坐上挥谈尘。",
        "更多情、多才多调，缓歌金缕。",
        "趁取芳时同宴赏，莫惜清樽缓举。",
        "有明月、随人归去。",
        "从此一春须一到，愿东君、长与花为主。",
        "泉共石，闻斯语。"
      ]
    },
    "2022-02-11": {
      "title": "鹧鸪天·正月十一日观灯",
      "author": "[南宋]姜夔",
      "body": [
        "巷陌风光纵赏时，笼纱未出马先嘶。",
        "白头居士无呵殿，只有乘肩小女随。",
        "花满市，月侵衣，少年情事老来悲。",
        "沙河塘上春寒浅，看了游人缓缓归。"
      ]
    },
    "2022-02-12": {
      "title": "永遇乐·落日熔金",
      "author": "[宋]李清照",
      "body": [
        "中州盛日，闺门多暇，记得偏重三五。",
        "铺翠冠儿，捻金雪柳，簇带争济楚。",
        "如今憔悴，风鬟霜鬓，怕见夜间出去。",
        "不如向、帘儿底下，听人笑语。"
      ]
    },
    "2022-02-13": {
      "title": "生查子·元夕",
      "author": "[宋]欧阳修",
      "body": [
        "去年元夜时，花市灯如昼。",
        "月上柳梢头，人约黄昏后。",
        "今年元夜时，月与灯依旧。",
        "不见去年人，泪湿春衫袖。"
      ]
    },
    "2022-02-14": {
      "title": "蓦山溪",
      "author": "[宋]欧阳修",
      "body": [
        "天涯情绪，对酒且开颜，",
        "春宵短。",
        "春寒浅。",
        "莫待金杯暖。"
      ]
    }
  }

  recs = []
  poems.each do |day, vs|
    rec = {}
    rec.store :day, day
    vs.each do |key, v|
      rec.store :title, v if key == :title
      rec.store :author, v if key == :author
      rec.store :background, v if key == :poster
      if key == :body
        content = v.join("<br>\n")
        rec.store :content, content
      end
    end
    rec.store :background, "https://mall.totembio.cn/uploads/dcc/DCC-BG-#{day}.png" if rec[:background].nil?
    recs += [rec]
  end

  return recs

end

def import_history_json
  recs = history_json_to_recs
  sqls = []
  sqls += ["delete from tutem_mall.eb_daily_poster where 1=1;"]
  sqls += ["alter table tutem_mall.eb_daily_poster auto_increment = 1;"]
  recs.each do |rec|
    sql = "insert into tutem_mall.eb_daily_poster(day,title,author,content,background,comment,add_time,admin_id)
            values('#{@rds.escape rec[:day].to_s}','#{@rds.escape rec[:title].to_s}','#{@rds.escape rec[:author].to_s}','#{@rds.escape rec[:content].to_s}','#{@rds.escape rec[:background].to_s}','imported from json lib',#{Time.now.to_i},19);"
    sqls += [sql]
  end
  commitTrans sqls
end
#import_history_json
